//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/.
// 

#include "Sink.h"
#include "message_k.h"
#include "info.h"
#include "message_m.h"

void PerformanceCounter::addDataTransmitInfo(DataTransmitInfo dataTransmitInfo)
{
	*outputFile << endl;

	*outputFile << "DataTransmitInfo #" << dataInfoCount << endl;
	*outputFile << "senderID: " << dataTransmitInfo.senderID << endl;
	*outputFile << "receiverID: " << dataTransmitInfo.receiverID << endl;
	*outputFile << "lossBitCount: " << dataTransmitInfo.lossBitCount << endl;
	*outputFile << "totalBitCount: " << dataTransmitInfo.totalBitCount << endl;
	*outputFile << "startTime: " << dataTransmitInfo.startTime << endl;
	*outputFile << "endTime: " << dataTransmitInfo.endTime << endl;
	*outputFile << "lossPacketCount: " << dataTransmitInfo.lossPacketCount << endl;
	*outputFile << "totalPacketCount: " << dataTransmitInfo.totalPacketCount << endl;

	*outputFile << endl;

	dataInfoCount++;
}

void PerformanceCounter::init(const char* outputFileName)
{
	outputFile = new std::ofstream(outputFileName, std::ios_base::out);
	dataInfoCount = 0;
}

PerformanceCounter::~PerformanceCounter()
{
	outputFile->close();
	delete outputFile;
}

Define_Module(Sink);

void Sink::initialize()
{
	outputFileName = par("outputFileName").stringValue();
	performanceCounter.init(outputFileName);
}

void Sink::handleMessage(cMessage *msg)
{
    switch(msg->getKind())
    {
    case DataTransmitInfoMsgKind:
		{
			DataTransmitInfoMsg *dataTransmitInfoMsg = check_and_cast<DataTransmitInfoMsg *>(msg);
			DataTransmitInfo dataTransmitInfo;
			dataTransmitInfo = dataTransmitInfoMsg->getDataTransmitInfo();
			performanceCounter.addDataTransmitInfo(dataTransmitInfo);
		}
    	break;

    }
}

